package com.ninthgroup.controller;

import com.ninthgroup.entity.Admin;
import com.ninthgroup.entity.Doctor;
import com.ninthgroup.entity.User;
import com.ninthgroup.service.AdminService;
import com.ninthgroup.service.DoctorService;
import com.ninthgroup.service.UserService;

import com.ninthgroup.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @param
 * @create by: 1452863884@qq.com rcg
 * @description: 登录控制
 * @create time: 2022/10/21 13:58
 * @return
 */
@RestController
@RequestMapping("/login")
@SessionAttributes(names = {"user","admin","doctor"})
public class LoginController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private UserService userService;

    @RequestMapping("/user")
    @ResponseBody
    public Result loginUser(@RequestBody User user, Model model, HttpSession session) {
        //病人登录
        User user1 = userService.findnameandpwd(user);

        model.addAttribute("user", user1);
        session.setAttribute("user", user1);
        if (user1 != null) {
            Result result = new Result(200, "登录成功");
            return result;
        }
        Result result = new Result(500, "登录失败");
        return result;

    }

    @RequestMapping("/admin")
    @ResponseBody
    public Result loginAdmin(@RequestBody Admin admin , Model model, HttpSession session) {
        //管理员登录
        Admin login = adminService.login(admin);

        model.addAttribute("admin", login);
        session.setAttribute("admin", login);
        if (login != null) {
            Result result = new Result(200, "登录成功");
            return result;
        }

        Result result = new Result(500, "登录失败");
        return result;

    }
    @RequestMapping("/doctor")
    @ResponseBody
    public Result loginDoctor(@RequestBody Doctor doctor, Model model, HttpSession session) {
        //医生登录
        Doctor doctor1 = doctorService.findnameandpwd(doctor);

        model.addAttribute("doctor", doctor1);
        session.setAttribute("doctor", doctor1);
        if (doctor1 != null) {
            Result result = new Result(200, "登录成功");
            return result;
        }

        Result result = new Result(500, "登录失败");
        return result;

    }






    @RequestMapping("/getuser")
    @ResponseBody
    public Result getusername(Model model) {

        User user = (User) model.getAttribute("user");
        Doctor doctor = (Doctor) model.getAttribute("doctor");
        Admin admin = (Admin) model.getAttribute("admin");
        if (user!=null){
            return new Result(5025, "获取session对象名字", user);
        }
        if (doctor!=null){
            return new Result(5025, "获取session对象名字", doctor);
        }
        if (admin!=null){
            return new Result(5025, "获取session对象名字", admin);
        }


       return new Result(500,"获取session失败");

    }








    @RequestMapping("/quit")
    @ResponseBody
    public Result layout(HttpSession session, SessionStatus sessionStatus, Model model) {
        //通过servleet的httpsession里面的方法来清空回话里面的数据
        session.removeAttribute("user");
        //测试是否清除
        System.out.println("logout:" + session.getAttribute("user"));
        //清除spring MVC model里面的session
        sessionStatus.setComplete();
        model.addAttribute("user", null);


        //通过servleet的httpsession里面的方法来清空回话里面的数据
        session.removeAttribute("doctor");
        //清除spring MVC model里面的session
        sessionStatus.setComplete();
        model.addAttribute("doctor", null);


        //通过servleet的httpsession里面的方法来清空回话里面的数据
        session.removeAttribute("admin");
        //清除spring MVC model里面的session
        sessionStatus.setComplete();
        model.addAttribute("admin", null);


        Result result = new Result(21, "退出当前用户");
        return result;
    }


}